'use strict';

angular.module('core')
    .directive('bindHideSubMenu', ['$document', function($document) {
        return {
            restrict: 'A',
            link: function($scope, $element) {
                
                $element.bind('click', stopEventPropagation);
                $document.bind('click', documentClick);

                $scope.$on('$destroy', function() {
                    $document.unbind('click', documentClick);
                });

                function stopEventPropagation(e) {
                    e.stopPropagation();
                }
                function documentClick(e) {
                    if (e.target.classList.contains('side-menu-item') ||
                        e.target.parentElement && e.target.parentElement.classList.contains('side-menu-item')
                    ) {
                        return;
                    }
                    $scope.data.subMenuVisible = false;
                    $scope.$apply();
                }
            }
        };
    }]);
